package Gui;

import java.util.HashMap;

import main.Main;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import Objects.Body;
import Objects.BodyCircle;
import Objects.BodyRectangle;
 
public class MainMenuState extends BasicGameState {
 
    int stateID = -1;
    Image background = null;
    Image startGameOption = null;
    Image exitOption = null;
    
    Image singleBody = null;
    Image twoBodies = null;
    Image rectangles = null;
    Image pool = null;
 
    float startGameScale = 1;
    float exitScale = 1;
    
    float singleScale = 1;
    float twoScale = 1;
    float rectangleScale = 1;
    float poolScale = 1;
    
    private static int menuX = 410;
    private static int menuY = 410;
    
    private static int defX = 20;
    private static int defY = 390;
    
    public HashMap<Integer, Body> bodies;
    public HashMap<Body, BodyRectangle> btr;
    public HashMap<Body, BodyCircle> btc;
    
    BodyCircle c1, c2, c3, c4, c5, c6, c7, c8, c9;
    BodyRectangle r1, r2;
    
    
    float scaleStep = 0.0001f;
 
    public MainMenuState(int stateID) 
    {
       this.stateID = stateID;
    }
 
    @Override
    public int getID() {
        return 0;
    }
 
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
    	background = new Image("/Images/physim.jpg");
    	
    	Image menuOptions = new Image("/Images/menuoptions.png");
    	
    	Image defaults = new Image ("/Images/defaults.png");
    	
    	startGameOption = menuOptions.getSubImage(0, 0, 377, 71);
    	exitOption = menuOptions.getSubImage(0, 71, 377, 71);
    	
    	singleBody = defaults.getSubImage(0, 00, 250, 45);
        twoBodies = defaults.getSubImage(0, 40, 250, 45);
        rectangles = defaults.getSubImage(0, 80, 250, 45);
        pool = defaults.getSubImage(0, 120, 250, 45);
    	
    	gc.setTargetFrameRate(60);
    }
 
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
    	// render the background
    	background.draw(0, 0);
    	 
    	// Draw menu
    	startGameOption.draw(menuX, menuY, startGameScale);
    	exitOption.draw(menuX, menuY+80, exitScale);
    	
    	singleBody.draw(defX, defY, singleScale);
        twoBodies.draw(defX, defY+55, twoScale);
        rectangles.draw(defX, defY+110, rectangleScale);
        pool.draw(defX, defY+165, poolScale);
    }
 
    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
    	Input input = gc.getInput();
    	 
    	int mouseX = input.getMouseX();
    	int mouseY = input.getMouseY();
    	 
    	boolean insideStartGame = false;
    	boolean insideExit = false;
    	
    	boolean insideSingle = false;
    	boolean insideTwo = false;
    	boolean insideRect = false;
    	boolean insidePool = false;
    	
    	
    	if( ( mouseX >= menuX && mouseX <= menuX + startGameOption.getWidth()) &&
    	    ( mouseY >= menuY && mouseY <= menuY + startGameOption.getHeight()) ){
    	    insideStartGame = true;
    	}
    	else if( ( mouseX >= menuX && mouseX <= menuX+ exitOption.getWidth()) &&
    	          ( mouseY >= menuY+80 && mouseY <= menuY+80 + exitOption.getHeight()) ){
    	    insideExit = true;
    	}
    	else if( (mouseX >= defX && mouseX <= defX + singleBody.getWidth()) && 
    			( mouseY >= defY && mouseY <= defY + singleBody.getHeight())){
    		insideSingle = true;
    	}
    	else if( (mouseX >= defX && mouseX <= defX + singleBody.getWidth()) && 
    			( mouseY >= defY+55 && mouseY <= defY+55 + singleBody.getHeight())){
    		insideTwo = true;
    	}
    	else if( (mouseX >= defX && mouseX <= defX + singleBody.getWidth()) && 
    			( mouseY >= defY+110 && mouseY <= defY+110 + singleBody.getHeight())){
    		insideRect = true;
    	}
    	else if( (mouseX >= defX && mouseX <= defX + singleBody.getWidth()) && 
    			( mouseY >= defY+165 && mouseY <= defY+165 + singleBody.getHeight())){
    		insidePool = true;
    	}
    	 
    	if(insideStartGame){
    	  if(startGameScale < 1.05f)
    	    startGameScale += scaleStep * delta;
    	 
    	  if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){
    	    sbg.enterState(Main.SETOBJECTSSTATE);
    	  }
    	}
    	else{
    	  if(startGameScale > 1.0f)
    	    startGameScale -= scaleStep * delta;
    	}
    	 
    	if(insideExit){
    	   if(exitScale < 1.05f)
    	     exitScale +=  scaleStep * delta;
    	   
    	   if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) )
       	    gc.exit();
    	}
    	else{
    	  if(exitScale > 1.0f)
    	    exitScale -= scaleStep * delta;
    	}
    	
    	if(insideSingle){
      	  if(singleScale < 1.05f)
      	    singleScale += scaleStep * delta;
      	 
      	  if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){
      		  
      		c1=new BodyCircle(1, 200, 100, 27 );
        	c1.setVx(15);
    		c1.setVy(-2);
    		bodies.put(bodies.size(),c1);
    		btc.put(bodies.get(bodies.size()-1),c1);
    		
    		sbg.enterState(Main.GAMEPLAYSTATE);
      	  }
      	}
      	else{
      	  if(singleScale > 1.0f)
      	    singleScale -= scaleStep * delta;
      	}
    	
    	if(insideTwo){
    		if(twoScale < 1.05f)
    			twoScale += scaleStep * delta;
        	 
        	if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){
            	c1=new BodyCircle(1, 150, 100, 27 );
	          	c1.setVx(15);
	      		c1.setVy(-2);
	      		bodies.put(bodies.size(),c1);
	      		btc.put(bodies.get(bodies.size()-1),c1);
	      		
	      		c2=new BodyCircle(1, 700, 200, 40 );
	          	c2.setVx(-7);
	      		c2.setVy(-5);
	      		bodies.put(bodies.size(),c2);
	      		btc.put(bodies.get(bodies.size()-1),c2);
     		    
        		sbg.enterState(Main.GAMEPLAYSTATE);
        	  }
        	}
        	else{
        	  if(twoScale > 1.0f)
        	    twoScale -= scaleStep * delta;
        	}
    	
    	if(insideRect){
    		if(rectangleScale < 1.05f)
    			rectangleScale += scaleStep * delta;
        	 
        	if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){
            	r1=new BodyRectangle(1, 150, 100, 40, 40);
	      		r1.setVx(15);
	      		r1.setVy(-2);
	      		r1.setAngularVelocity(0);
	      		bodies.put(bodies.size(),r1);
	      		btr.put(bodies.get(bodies.size()-1),r1);
	      		
	      		r2=new BodyRectangle(1, 700, 200, 40, 40);
	      		r2.setVx(-7);
	      		r2.setVy(-5);
	      		r2.setAngularVelocity(0);
	      		bodies.put(bodies.size(),r2);
	      		btr.put(bodies.get(bodies.size()-1),r2);
        		  
        		sbg.enterState(Main.GAMEPLAYSTATE);
        	  }
        	}
        	else{
        	  if(rectangleScale > 1.0f)
        	    rectangleScale -= scaleStep * delta;
        	}
    	
    	if(insidePool){
    		if(poolScale < 1.05f)
    			poolScale += scaleStep * delta;
        	 
        	if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){
        		        		
        		c1=new BodyCircle(1, 200, 80, 20, 0, 0, 0, 0, 0, -10, 0);
	          	bodies.put(bodies.size(),c1);
	      		btc.put(bodies.get(bodies.size()-1),c1);
	      		
	      		c2=new BodyCircle(1, 200, 120, 20, 0, 0, 0, 0, 0, -10, 0);
	          	bodies.put(bodies.size(),c2);
	      		btc.put(bodies.get(bodies.size()-1),c2);
	      		
	      		c3=new BodyCircle(1, 240, 100, 20, 0, 0, 0, 0, 0, -10, 0);
	          	bodies.put(bodies.size(),c3);
	      		btc.put(bodies.get(bodies.size()-1),c3);
	      		
	      		c4=new BodyCircle(1, 100, 100, 20, 0, 0, 0, 0, 15, -10, 0);
	          	bodies.put(bodies.size(),c4);
	      		btc.put(bodies.get(bodies.size()-1),c4);
        		  
        		sbg.enterState(Main.GAMEPLAYSTATE);
        	  }
        	}
        	else{
        	  if(poolScale > 1.0f)
        	    poolScale -= scaleStep * delta;
        	}
      }
}